package com.ruoyi.business.controller;

import com.ruoyi.business.rabbitmq.AcquisitionProtocolConstants;
import com.ruoyi.business.rabbitmq.listener.SignalListener;
import com.ruoyi.business.rabbitmq.po.CollectDataInfo;
import com.ruoyi.business.rabbitmq.po.SignalInfo;
import com.ruoyi.business.rabbitmq.publisher.Publisher;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.sun.corba.se.spi.orb.DataCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@RestController
@RequestMapping("/rabbit")
public class RabbitTestController {

    @Autowired
    private Publisher publisher;

    @Autowired
    private SignalListener signalListener;

    @GetMapping("/startCollect")
    public AjaxResult startCollect() {
        try {
            signalListener.initCache();
            SignalInfo signalInfo = new SignalInfo();
            signalInfo.setCmd(AcquisitionProtocolConstants.DISPATCH_SIGNAL_START);
            signalInfo.setDelay(0);
            signalInfo.setDispatchTime(DateUtils.getNowDate());
            publisher.publishSignal(AcquisitionProtocolConstants.SIGNAL_EXCHANGER_NAME, signalInfo);
        } catch (Exception e) {
            System.out.println(e);
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @GetMapping("/stopCollect")
    public AjaxResult stopCollect() {
        try {
            SignalInfo signalInfo = new SignalInfo();
            signalInfo.setCmd(AcquisitionProtocolConstants.DISPATCH_SIGNAL_STOP);
            signalInfo.setDelay(0);
            signalInfo.setDispatchTime(DateUtils.getNowDate());
            publisher.publishSignal(AcquisitionProtocolConstants.SIGNAL_EXCHANGER_NAME, signalInfo);
            signalListener.initCacheTimer();
        } catch (Exception e) {
            System.out.println(e);
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @GetMapping("/mockData")
    public AjaxResult mockData() {
        try {
            List<CollectDataInfo> datas = new ArrayList<>();
            CollectDataInfo collectDataInfo = new CollectDataInfo();
            collectDataInfo.setCollectTime(DateUtils.getNowDate().getTime());
            collectDataInfo.setValue("1");
            collectDataInfo.setChannel("1");
            datas.add(collectDataInfo);
            publisher.publishData(AcquisitionProtocolConstants.DATA_EXCHANGER_NAME, datas);
        } catch (Exception e) {
            System.out.println(e);
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
